package app.engine.solucoes.request.servico;

import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import app.engine.solucoes.request.beans.Usuario;


public class EmailService {

	public void sendMail(List<Usuario> destinatarios, String msgBody, String assunto) {

		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);

		try {
			Message msg = new MimeMessage(session);
			InternetAddress enderecos[] = new  InternetAddress[destinatarios.size()];
			for (int i = 0; i < enderecos.length; i++) {
				Usuario usuario = destinatarios.get(i);
				enderecos[i] = new InternetAddress(usuario.getEmail());
			}
			msg.setFrom(new InternetAddress("gleidson.gmoura@gmail.com"));
			msg.addRecipients(Message.RecipientType.TO, enderecos);
			msg.setSubject(assunto);
			msg.setText(msgBody);
			Transport.send(msg);

		} catch (AddressException e) {
			System.err.println("erro ao tentar enviar email");
		} catch (MessagingException e) {
			System.err.println("erro ao tentar enviar email");
		} catch (Exception e) {
			System.err.println("erro ao tentar enviar email");
		}
	}
}
